<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
  User,
  Fold,
  Expand,
  CaretRight,
  Sunny,
  Moon
} from '@element-plus/icons-vue'

// 侧边栏展开状态
const isCollapse = ref(false)

// 主题模式 - light 或 dark
const themeMode = ref('light')

// 更新侧边栏菜单数据，增加大屏设计器菜单项
const menuItems = ref([
  {
    id: 1,
    title: '仪表盘',
    icon: Document,
    path: '/dashboard'
  },
  {
    id: 2,
    title: '用户管理',
    icon: User,
    children: [
      {
        id: 21,
        title: '用户列表',
        path: '/users'
      },
      {
        id: 22,
        title: '用户详情',
        path: '/user-management'
      }
    ]
  },
  {
    id: 3,
    title: '系统管理',
    icon: Setting,
    children: [
      {
        id: 31,
        title: '菜单管理',
        path: '/menu'
      },
      {
        id: 32,
        title: '角色管理',
        path: '/role'
      },
      {
        id: 33,
        title: '子系统',
        children: [
          {
            id: 331,
            title: '子系统1',
            path: '/sub1'
          },
          {
            id: 332,
            title: '子系统2',
            path: '/sub2'
          }
        ]
      }
    ]
  },
  // 添加GIS功能菜单项
  {
    id: 4,
    title: '地图功能',
    icon: Location,
    path: '/gis'
  },
  // 添加低代码设计器菜单项
  {
    id: 5,
    title: '低代码平台',
    icon: Document,
    path: '/low-code'
  },
  // 添加大屏设计器菜单项
  {
    id: 6,
    title: '大屏设计器',
    icon: Document,
    path: '/big-screen'
  },
  // 添加旅游商品管理菜单项
  {
    id: 7,
    title: '旅游商品管理',
    icon: Document,
    path: '/tourism-products'
  }
])

const router = useRouter()
const route = useRoute()
const activeIndex = ref('/dashboard')

// 菜单点击处理
const handleMenuSelect = (path) => {
  if (path) {
    activeIndex.value = path
    router.push(path)
  }
}

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 切换主题模式
const toggleTheme = () => {
  themeMode.value = themeMode.value === 'light' ? 'dark' : 'light'
}

// 计算侧边栏宽度
const sidebarWidth = computed(() => {
  return isCollapse.value ? '64px' : '200px'
})

// 监听主题变化并应用到 Element Plus
watch(themeMode, (newMode) => {
  // 移除所有主题类
  document.documentElement.classList.remove('dark', 'light')
  // 添加当前主题类
  document.documentElement.classList.add(newMode)
  
  // 更新 Element Plus 主题变量
  const root = document.documentElement
  if (newMode === 'dark') {
    // 暗色主题变量
    root.style.setProperty('--el-bg-color', '#141414')
    root.style.setProperty('--el-bg-color-page', '#0a0a0a')
    root.style.setProperty('--el-text-color-primary', '#e5eaf3')
    root.style.setProperty('--el-text-color-regular', '#cfd3dc')
    root.style.setProperty('--el-border-color', '#4c4d4f')
    root.style.setProperty('--el-fill-color', '#1d1e1f')
    root.style.setProperty('--el-fill-color-light', '#262727')
    root.style.setProperty('--el-fill-color-blank', '#141414')
  } else {
    // 亮色主题变量 - 移除覆盖值以使用默认值
    root.style.removeProperty('--el-bg-color')
    root.style.removeProperty('--el-bg-color-page')
    root.style.removeProperty('--el-text-color-primary')
    root.style.removeProperty('--el-text-color-regular')
    root.style.removeProperty('--el-border-color')
    root.style.removeProperty('--el-fill-color')
    root.style.removeProperty('--el-fill-color-light')
    root.style.removeProperty('--el-fill-color-blank')
  }
}, { immediate: true })
</script>

<template>
  <div class="common-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="sidebarWidth" class="sidebar-container">
        <div class="logo" :class="{ 'collapse': isCollapse }">
          <transition name="sidebar-logo-fade" mode="out-in">
            <router-link v-if="isCollapse" key="collapse" to="/">
              <h2 class="sidebar-logo">M</h2>
            </router-link>
            <router-link v-else key="expand" to="/">
              <h2 class="sidebar-title">管理系统</h2>
            </router-link>
          </transition>
        </div>
        <el-menu
          :default-active="route.path"
          class="el-menu-vertical-demo"
          :collapse="isCollapse"
          :collapse-transition="true"
          @select="handleMenuSelect"
        >
          <template v-for="item in menuItems" :key="item.id">
            <el-sub-menu 
              v-if="item.children && item.children.length > 0" 
              :index="item.path || item.id.toString()"
            >
              <template #title>
                <el-icon><component :is="item.icon" /></el-icon>
                <span>{{ item.title }}</span>
              </template>
              <template v-for="child in item.children" :key="child.id">
                <el-sub-menu 
                  v-if="child.children && child.children.length > 0" 
                  :index="child.path || child.id.toString()"
                >
                  <template #title>
                    <el-icon><CaretRight /></el-icon>
                    <span>{{ child.title }}</span>
                  </template>
                  <el-menu-item 
                    v-for="grandChild in child.children" 
                    :key="grandChild.id" 
                    :index="grandChild.path"
                  >
                    <span>{{ grandChild.title }}</span>
                  </el-menu-item>
                </el-sub-menu>
                <el-menu-item 
                  v-else 
                  :index="child.path"
                >
                  <el-icon><component :is="child.icon" /></el-icon>
                  <span>{{ child.title }}</span>
                </el-menu-item>
              </template>
            </el-sub-menu>
            <el-menu-item 
              v-else 
              :index="item.path"
            >
              <el-icon><component :is="item.icon" /></el-icon>
              <template #title>
                <span>{{ item.title }}</span>
              </template>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>
      
      <el-container>
        <!-- 头部 -->
        <el-header class="navbar">
          <div class="navbar-left">
            <div class="hamburger-container" @click="toggleCollapse">
              <el-icon v-if="isCollapse">
                <Expand />
              </el-icon>
              <el-icon v-else>
                <Fold />
              </el-icon>
            </div>
            <div class="breadcrumb-container">
              <!-- 面包屑导航可以在这里添加 -->
            </div>
          </div>
          <div class="navbar-right">
            <!-- 主题切换按钮 -->
            <div class="theme-toggle" @click="toggleTheme">
              <el-icon v-if="themeMode === 'light'">
                <Moon />
              </el-icon>
              <el-icon v-else>
                <Sunny />
              </el-icon>
            </div>
            <div class="user-info">
              <el-dropdown>
                <span class="el-dropdown-link">
                  管理员
                  <el-icon class="el-icon--right">
                    <arrow-down />
                  </el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>个人信息</el-dropdown-item>
                    <el-dropdown-item divided>退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        
        <!-- 主要内容区域 -->
        <el-main>
          <router-view v-slot="{ Component }">
            <transition name="fade-transform" mode="out-in">
              <component :is="Component" />
            </transition>
          </router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style scoped>
.common-layout {
  height: 100vh;
}

.sidebar-container {
  background-color: var(--sidebar-bg-color);
  color: var(--sidebar-text-color);
  height: 100%;
  transition: width 0.28s ease-out;
  position: relative;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  z-index: 1001;
}

.logo {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  overflow: hidden;
  transition: all 0.3s ease-out;
  background-color: var(--sidebar-logo-bg-color);
}

.logo.collapse {
  padding: 0;
  justify-content: center;
}

.sidebar-logo {
  font-size: 20px;
  font-weight: 600;
  color: var(--sidebar-logo-text-color);
  margin: 0;
}

.sidebar-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--sidebar-logo-text-color);
  margin: 0;
  white-space: nowrap;
}

.el-menu {
  border-right: none;
  background-color: var(--sidebar-bg-color);
  height: calc(100% - 50px);
}

:deep(.el-menu-item) {
  color: var(--sidebar-menu-text-color);
}

:deep(.el-menu-item):hover {
  background-color: var(--sidebar-menu-hover-bg-color) !important;
}

:deep(.el-menu-item.is-active) {
  background-color: var(--sidebar-menu-active-bg-color) !important;
  color: var(--sidebar-menu-active-text-color) !important;
}

:deep(.el-sub-menu .el-menu-item) {
  color: var(--sidebar-menu-text-color);
}

:deep(.el-sub-menu .el-menu-item):hover {
  background-color: var(--sidebar-menu-hover-bg-color) !important;
}

:deep(.el-sub-menu .el-menu-item.is-active) {
  background-color: var(--sidebar-menu-active-bg-color) !important;
  color: var(--sidebar-menu-active-text-color) !important;
}

:deep(.el-menu-item.is-active) {
  position: relative;
}

:deep(.el-menu-item.is-active)::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background-color: var(--el-color-primary);
}

.navbar {
  background-color: var(--navbar-bg-color);
  color: var(--navbar-text-color);
  border-bottom: 1px solid var(--navbar-border-color);
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  z-index: 999;
  position: relative;
}

.navbar-left {
  display: flex;
  align-items: center;
}

.hamburger-container {
  padding: 0 15px;
  height: 100%;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: background 0.3s;
}

.hamburger-container:hover {
  background: var(--navbar-hover-bg-color);
}

.breadcrumb-container {
  margin-left: 10px;
}

.navbar-right {
  display: flex;
  align-items: center;
  margin-right: 20px;
}

.theme-toggle {
  margin-right: 20px;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  transition: background-color 0.3s;
  color: var(--navbar-icon-color);
}

.theme-toggle:hover {
  background-color: var(--navbar-icon-hover-bg-color);
}

.user-info {
  display: flex;
  align-items: center;
  color: var(--navbar-text-color);
}

/* 修改主要内容区域样式，使其自适应并正确处理滚动 */
.el-main {
  background-color: var(--main-bg-color);
  padding: 20px;
  height: calc(100vh - 50px);
  overflow-y: auto;
}

.sidebar-logo-fade-enter-active {
  transition: opacity 0.2s ease-out;
}

.sidebar-logo-fade-leave-active {
  transition: opacity 0.2s ease-out;
}

.sidebar-logo-fade-enter-from,
.sidebar-logo-fade-leave-to {
  opacity: 0;
}

.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all 0.3s ease-out;
}

.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}

.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
</style>

<style>
/* CSS 变量定义 */
:root {
  /* 亮色主题变量 - 蓝色为主色调 */
  --sidebar-bg-color: #1890ff;
  --sidebar-text-color: #fff;
  --sidebar-logo-bg-color: #40a9ff;
  --sidebar-logo-text-color: #fff;
  --sidebar-menu-text-color: #e6f7ff;
  --sidebar-menu-hover-bg-color: #40a9ff;
  --sidebar-menu-active-bg-color: #096dd9;
  --sidebar-menu-active-text-color: #ffffff;
  --navbar-bg-color: #fff;
  --navbar-text-color: #333;
  --navbar-border-color: #eee;
  --navbar-hover-bg-color: rgba(0, 0, 0, 0.025);
  --navbar-icon-color: #333;
  --navbar-icon-hover-bg-color: #f5f5f5;
  --main-bg-color: #f0f2f5;
}

/* 暗色主题变量 */
html.dark {
  --sidebar-bg-color: #1a1b1e;
  --sidebar-text-color: #e5eaf3;
  --sidebar-logo-bg-color: #1a1b1e;
  --sidebar-logo-text-color: #e5eaf3;
  --sidebar-menu-text-color: #cfd3dc;
  --sidebar-menu-hover-bg-color: #2a2b2f;
  --sidebar-menu-active-bg-color: #409eff;
  --sidebar-menu-active-text-color: #ffffff;
  --navbar-bg-color: #141414;
  --navbar-text-color: #e5eaf3;
  --navbar-border-color: #4c4d4f;
  --navbar-hover-bg-color: rgba(255, 255, 255, 0.025);
  --navbar-icon-color: #e5eaf3;
  --navbar-icon-hover-bg-color: #262727;
  --main-bg-color: #0a0a0a;
}
</style>